package dao.impl;

import dao.AnimeDao;
import dao.BaseDao;
import dao.RatingDao;
import dao.UserDao;
import dao.impl.AnimeDaoImplement;
import dao.impl.UserDaoImplement;
import pojo.Anime;
import pojo.User;

import java.sql.PreparedStatement;
import java.util.List;

public class RatingDaoImplement extends BaseDao implements RatingDao{
    private UserDao userDao=new UserDaoImplement();
    private AnimeDao animeDao=new AnimeDaoImplement();
    @Override
    public String doRating(String user_ID, String anime_ID, int rat) {
       String wrongMsg="success";
       /* 由于user_id 是我们保留的所以user_ID一定会存在*/

        //更新anime表
        Anime anime=animeDao.queryAnimebyId(anime_ID);

       if(anime==null){
           wrongMsg="404";
           return wrongMsg;
       }
       String sqlOne=" update rat_anime set" +
               " rating=CAST(CAST(round(rating +( ?-rating)/(members+1),2) as numeric(10,2))as FLOAT) " +
               "where anime_id = ?";
       update(sqlOne,rat,anime_ID);
       String sqlTwo="update rat_anime set memebers=members+1 where anime_id=?";
       update(sqlTwo,anime_ID);
       //处理插入rat_rating表的逻辑
        String sql="insert into rat_rating values(?,?,?)";
       //判断是否插入异常,如果正常执行会返回更新的行数
        int flag=update(sql,user_ID,anime_ID,rat);
       return wrongMsg;
    }
}
